﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Artem.Web.UI.Controls;

using NotesFor;

using SLIITPro.Controls;
using SLIITPro.Data.Extension;
using SLIITPro.Data.Entity;
using SLIITPro.Core;
using System.IO;

namespace SLIITPro
{
    public partial class PlacesInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                if (Session["id"] == null || Session["id"].ToString() != Request.QueryString["id"])
                {
                    Session["id"] = Request.QueryString["id"];
                }

            if (Session["id"] == null || !Utility.IsNumberId(Session["id"].ToString())) return;
            GetBusinessItem(int.Parse(Session["id"].ToString()));
        }

        private Gallery _imageGallery;
        private BusinessItem _item;
        private BusinessItemExtension _itemExtension;
        private void GetBusinessItem(int itemId)
        {
            if (string.IsNullOrEmpty(itemId.ToString())) return;
            try
            {
                _itemExtension = new BusinessItemExtension();
                var table = _itemExtension.SelectBusiness(itemId);
                _item = new BusinessItem(table.Rows[0]);
                lblBusinessName.Text = _item.BName;
                lblCity.Text = _item.BCity;
                lblStreet.Text = _item.BStreet;
                lblPhone.Text = _item.BPhone;
                lblEmail.Text = _item.BEmail;
                
                SetGoogleMap();
                if (!string.IsNullOrEmpty(_item.BImageFolderID))
                {
                    var path = CoreSettings.GlobalBusinessImageRoot + _item.BImageFolderID + "/";
                    if (Directory.Exists(Server.MapPath(path)))
                    {
                        _imageGallery = new Gallery
                        {
                            FolderUrl = CoreSettings.GlobalBusinessImageRoot + _item.BImageFolderID,
                            Size = 100,
                            AlbumTitle = _item.BName
                        };
                        ImageGallery.Controls.Add(_imageGallery);
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }

        private void SetGoogleMap()
        {
            string address = string.Concat(_item.BCity, ",", Utility.GetCountry(_item.BCountry));
            GoogleMapBusiness.Address = address;
            GoogleMapBusiness.Markers.Clear();
            GoogleMapBusiness.Markers.Add(new GoogleMarker(address));
        }

    }
}
